/*************************************************************************
 * File Name:    Anagrams.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/16 15:16:17
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Anagrams
 |
 | Given an array of strings, return all groups of strings that are anagrams.  
 |
 | Note: All inputs will be in lower-case.  
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    vector<string> anagrams(vector<string> &strs)
    {
        vector<string> ans;
        map<string, vector<string> > dict;
        
        for (size_t i = 0; i < strs.size(); ++i) {
            string t = strs[i];
            sort(t.begin(), t.end());
            dict[t].push_back(strs[i]);
        }
        for (map<string, vector<string> >::iterator it = dict.begin(); it != dict.end(); ++it) {
            if (it->second.size() > 1) {
                ans.insert(ans.end(), it->second.begin(), it->second.end());
            }
        }
        return ans;
    }
};

